//
//  SongDetailViewController.m
//  NhomThuy02
//
//  Created by admin on 2/2/15.
//  Copyright (c) 2015 LamQuoc. All rights reserved.
//

#import "SongDetailViewController.h"


@interface SongDetailViewController ()
@property (strong, nonatomic) IBOutlet UILabel *lblSongID;
@property (strong, nonatomic) IBOutlet UILabel *lblSongTitle;
@property (strong, nonatomic) IBOutlet UILabel *lblSingerName;
@property (strong, nonatomic) IBOutlet UITextView *tvLyric;
@property BOOL likeStatus;


@end

@implementation SongDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
   self.navigationController.navigationBar.topItem.title = @"Trở về";
    NSString *songID = [self.songData objectForKey:@"song-code"];
    self.lblSongID.text = songID;
    self.lblSongTitle.text = [[self.songData objectForKey:@"song-name"]capitalizedString];
    self.tvLyric.text = [self.songData  objectForKey:@"lyrics"];
    self.lblSingerName.text = [[ManagerData defaultManager]getSingerNameBySingerID:[self.songData objectForKey:@"singer-id"]];
    
    NSString* iconImage;
    if([[ManagerData defaultManager]isAlreadyLikeSong:songID])
    {
        iconImage = @"icon_navigation_bar_already_like.png";
        self.likeStatus = YES;
    }
    else
    {
        iconImage = @"icon_navigation_bar_not_like.png";
        self.likeStatus = NO;
    }
    
    [self setRightButtonLayout:iconImage];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILongPressGestureRecognizer* _longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self.overlay action:@selector(longPressDetected:)];
    [self.btnMenu addGestureRecognizer:_longPressRecognizer];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)onRightButtonClick
{
    self.likeStatus = !self.likeStatus;
    
    if (self.likeStatus) {
        [self setRightButtonLayout:@"icon_navigation_bar_already_like.png"];
        [[ManagerData defaultManager]addLoveSongWithDictionaryOfSong:self.songData];
        self.likeStatus = YES;
    }
    else
    {
        NSString *songName = [self.songData objectForKey:@"song-name"];
        NSString *alertContent = [NSString stringWithFormat:@"Bạn có đồng ý bỏ thích bài hát \n%@",songName];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Bỏ thích bài hát" message:alertContent delegate:self cancelButtonTitle:@"Huỷ" otherButtonTitles:@"Đồng ý",nil];
        [alertView show];
    }
}

- (void)setRightButtonLayout:(NSString*)image
{
    UIButton *btnLike = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *btnLikeImage = [UIImage imageNamed:image]  ;
    [btnLike setBackgroundImage:btnLikeImage forState:UIControlStateNormal];
    [btnLike addTarget:self action:@selector(onRightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    btnLike.frame = CGRectMake(0, 0, 30, 30);
    UIBarButtonItem *btnBarLike = [[UIBarButtonItem alloc] initWithCustomView:btnLike] ;
    self.navigationItem.rightBarButtonItem = btnBarLike;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        self.likeStatus = !self.likeStatus;
    }
    else
    {
        [self setRightButtonLayout:@"icon_navigation_bar_not_like.png"];
        [[ManagerData defaultManager]removeLoveSongWithDictionaryOfSong:self.songData];
        self.likeStatus = NO;
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
